<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Moment.js Guides - Moment.js Guides | Docs4dev</title>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="Parse, validate, manipulate, and display dates and times in JavaScript.">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="HandheldFriendly" content="true">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta property="og:type" content="website">
<meta property="og:title" content="Moment.js Guides - Moment.js Guides">
<meta property="og:url" content="https://www.docs4dev.com/docs/en/momentjs/2.24.0/guides/">
<meta property="og:site_name" content="Docs4dev">
<meta property="og:locale" content="zh_CN">
<meta name="twitter:card" content="Parse, validate, manipulate, and display dates and times in JavaScript.">
<meta name="generator" content="Docs4dev template engine">
<link rel="stylesheet" href="static/css/app.min.css">
<link rel="shortcut icon" href="https://www.docs4dev.com/static/images/favicon.ico" type="image/x-icon">
<script async="" src="static/js/js.js"></script>
<script async="" src="static/js/adsbygoogle.js" crossorigin="anonymous"></script>
<script>
    window.dataLayer = window.dataLayer || [];

    function gtag() {
      dataLayer.push(arguments);
    }

    gtag('js', new Date());
    gtag('config', 'UA-129571937-1');
  </script>
<link rel="amphtml" href="https://www.docs4dev.com/amp/docs/en/momentjs/2.24.0/guides/index.html">

<script type="application/ld+json">{"name":null,"headline":"Moment.js Guides-Moment.js Guides","inLanguage":"en-US","version":"2.24.0","image":"/static/icon/icon-momentjs.png","datePublished":"2021-05-20T12:51:01Z","dateCreated":"2021-05-20T12:51:01Z","dateModified":"2021-07-03T12:24:36Z","@context":"https://schema.org/","@type":"APIReference","abstract":"Parse, validate, manipulate, and display dates and times in JavaScript."}</script>
</head>
<body>
<div class="book with-summary">
<div class="book-summary">
<div class="logo">
<a href="javascript:window.open('https://www.docs4dev.com/docs/en/momentjs/2.24.0/guides');" style="color: inherit;">
<img src="static/picture/icon-momentjs.png" style="width: 48px; height: 48px;" alt="Logo">
</a>
<b style="color: inherit; margin-left: 8px;">Moment.js Guides</b>
</div>
<div class="item">
<div>
<label for="version">版本</label>
<select id="version" onchange="onVersionChange(this)">
<option value="2.24.0" selected="selected">2.24.0</option>
</select>
</div>
<div style="margin-top: 8px;">
<label for="language">语言</label>
<select id="language" onchange="onLangChange(this)" value="en">
<option value="en" selected="selected">English</option>
<option value="zh">中文</option>
</select>
</div>

</div>
<div class="item menus">
<a title="Table of Contents" style="margin-right: 8px;" href="#">
<i class="fa fa-chevron-left"></i>
<span style="margin-left: 2px;">返回目录</span>
</a>

</div>
<nav role="navigation" id="navigation">
<ul class="summary">
<li class="chapter"> <a href="javascript:window.open('https://www.docs4dev.com/docs/en/momentjs/2.24.0/guides/index.html#/lib-concepts/');" title="Introductory Concepts"> Introductory Concepts </a> </li>
<li class="chapter"> <a href="javascript:window.open('https://www.docs4dev.com/docs/en/momentjs/2.24.0/guides/index.html#/lib-concepts/mutability/');" title="Mutability"> Mutability </a> </li>
<li class="chapter"> <a href="javascript:window.open('https://www.docs4dev.com/docs/en/momentjs/2.24.0/guides/index.html#/lib-concepts/date-time-math/');" title="Date Math vs Time Math"> Date Math vs Time Math </a> </li>
<li class="chapter"> <a href="javascript:window.open('https://www.docs4dev.com/docs/en/momentjs/2.24.0/guides/index.html#/lib-concepts/timezone-offset/');" title="Time Zone vs Offset"> Time Zone vs Offset </a> </li>
<li class="chapter"> <a href="javascript:window.open('https://www.docs4dev.com/docs/en/momentjs/2.24.0/guides/index.html#/lib-concepts/js-date/');" title="JavaScript Date"> JavaScript Date </a> </li>
<li class="chapter"> <a href="javascript:window.open('https://www.docs4dev.com/docs/en/momentjs/2.24.0/guides/index.html#/lib-concepts/internal-properties/');" title="Internal Properties"> Internal Properties </a> </li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<header class="book-header">
<div class="dropdown pull-right js-toolbar-action">
<a class="btn toggle-dropdown" aria-label="Language" href="#">
<i class="fa fa-globe"></i>
</a>
<div class="dropdown-menu dropdown-left">
<div class="dropdown-caret"><span class="caret-outer"></span><span class="caret-inner"></span></div>
<div class="buttons">
<button class="button size-1" onclick="changeLang('zh_CN')">中文</button>
</div>
<div class="buttons">
<button class="button size-1" onclick="changeLang('en_US')">English</button>
</div>
</div>
</div>
<a class="btn pull-right js-toolbar-action non-mobile" aria-label="Sign up" href="register.html">
<i class="fa fa-sign-in"></i> <span>注册</span>
</a>
<a class="btn pull-right js-toolbar-action non-mobile" aria-label="Sign in" href="register.html">
<i class="fa fa-sign-in"></i>
<span>登录</span>
</a>
<a class="btn pull-left js-toolbar-action btn-summary" href="#"><i class="fa fa-align-justify"></i></a>
<div class="dropdown pull-left font-settings js-toolbar-action">
<a class="btn toggle-dropdown" aria-label="Font Settings" href="#">
<i class="fa fa-font"></i>
</a>
<div class="dropdown-menu dropdown-right">
<div class="dropdown-caret"><span class="caret-outer"></span><span class="caret-inner"></span></div>
<div class="buttons">
<button class="button size-2 font-reduce">A</button>
<button class="button size-2 font-enlarge">A</button>
</div>
<div class="buttons">
<button class="button size-2 family-serif">Serif</button>
<button class="button size-2 family-sans">Sans</button>
</div>
<div class="buttons">
<button class="button size-3 theme-white">White</button>
<button class="button size-3 theme-sepia">Sepia</button>
<button class="button size-3 theme-night">Night</button>
</div>
</div>
</div>
<a class="btn pull-left js-toolbar-action non-mobile" aria-label="Home" href="en.html">
<i class="fa fa-home"></i> <span>首页</span>
</a>
<a class="btn pull-left js-toolbar-action non-mobile" aria-label="Guide" href="javascript:window.open('https://www.javadoc.org/');">
<i class="fa fa-book"></i> <span>API Docs</span>
</a>
<a class="btn pull-left js-toolbar-action non-mobile" aria-label="Tools" href="index37.html">
<i class="fa fa-gears"></i> <span>工具</span>
</a>
<div class="dropdown pull-left js-toolbar-action mobile">
<a class="btn toggle-dropdown" aria-label="Language" href="#">
<i class="fa fa-chevron-down"></i>
</a>
<div class="dropdown-menu dropdown-right">
<div class="dropdown-caret"><span class="caret-outer"></span><span class="caret-inner"></span></div>
<div class="buttons">
<a class="button size-1" aria-label="Home" href="en.html">
<i class="fa fa-home"></i> <span>首页</span>
</a>
</div>
<div class="buttons">
<a class="button size-1" aria-label="Guide" href="javascript:window.open('https://www.javadoc.org/');">
<i class="fa fa-book"></i> <span>API Docs</span>
</a>
</div>
<div class="buttons">
<a class="button size-1" aria-label="Tools" href="index37.html">
<i class="fa fa-gears"></i> <span>工具</span>
</a>
</div>
</div>
</div>
<div id="autocomplete" class="pull-right"></div>
<span id="toolbar-title"></span>
</header>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal markdown-section">
<div id="content">
<h1>Moment.js Guides</h1>
<div><ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-6108808167664152" data-ad-slot="6964403648"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div><article class="docs-section">
<a class="docs-section-target" id="/lib-concepts/" name="/lib-concepts/"></a>
<h2 class="docs-section-title" id="Introductory-Concepts"><a href="#/lib-concepts/">Introductory Concepts</a></h2>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/lib-concepts/intro/" name="/lib-concepts/intro/"></a>
<div class="docs-method-prose">
<p>The guides area is designed to help developers learn to better interact with the date and time problem domain, and the Moment.js library.<br> We address our most frequently seen support requests here, so it is a great place to check for solutions to any issues you may have.</p>
<p>The guides section is new and still under construction. If you have a request for a guide that you would like to see here, or would like to add a guide<br> please create an issue or make a pull request in the <a href="javascript:window.open('https://github.com/moment/momentjs.com');" target="_blank" rel="noopener noreferrer">momentjs.com repository <i class="fa fa-external-link"></i></a>.</p>
<p>Check out this <a href="javascript:window.open('https://scrimba.com/casts/cwpDGhG');" target="_blank" rel="noopener noreferrer">scrimba moment guide <i class="fa fa-external-link"></i></a> if you're just starting.</p>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/lib-concepts/mutability/" name="/lib-concepts/mutability/"></a>
<h3 class="docs-method-title" id="Mutability-1.0.0+"> <a href="#/lib-concepts/mutability/">Mutability</a> <span>1.0.0+</span> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/00-lib-concepts/01-mutability.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<p>The moment object in Moment.js is mutable. This means that operations like add, subtract, or set change the original moment object.<br> When first using Moment.js many developers are confused by scenarios like this:</p>
<pre class="notranslate"><code class="language-js">var a = moment('2016-01-01'); 
var b = a.add(1, 'week'); 
a.format();
"2016-01-08T00:00:00-06:00"
</code></pre>
<p>As you can see, adding one week mutated <code class="notranslate">a</code>. To avoid situations like that, clone the moment before performing date math:</p>
<pre class="notranslate"><code class="language-js">var a = moment('2016-01-01'); 
var b = a.clone().add(1, 'week'); 
a.format();
"2016-01-01T00:00:00-06:00"
</code></pre>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/lib-concepts/date-time-math/" name="/lib-concepts/date-time-math/"></a>
<h3 class="docs-method-title" id="Date-Math-vs-Time-Math"> <a href="#/lib-concepts/date-time-math/">Date Math vs Time Math</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/00-lib-concepts/02-date-time-math.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<p>There is a logical difference between time math and date math.</p>
<p>In Moment.js time math assumes a linear time scale, just incrementing or decrementing the UTC-based timestamp by the amount of the time units provided.</p>
<p>Date math does not use a linear time scale, but rather increments or decrements the dates on the calendar. This is because the amount of time in a day, month, or year is variable.<br> For example, due to daylight saving time transition, a day may be anywhere between 23 and 25 hours long.<br> Months of course vary in number of days, and due to leap year, years vary in length as well. Date math can cause some interesting scenarios.</p>
<p>Due to daylight saving time, one day may not equal 24 hours:</p>
<pre class="notranslate"><code class="language-js">//date math
moment('2016-03-12 13:00:00').add(1, 'day').format('LLL')
"March 13, 2016 1:00 PM"
//time math
moment('2016-03-12 13:00:00').add(24, 'hours').format('LLL')
"March 13, 2016 2:00 PM"
</code></pre>
<p>Due to leap years, one year may not equal 365 days:</p>
<pre class="notranslate"><code class="language-js">moment('2016-01-01').add(1, 'year').format('LL')
"January 1, 2017"
moment('2016-01-01').add(365, 'day').format('LL')
"December 31, 2016"
</code></pre>
<p>Because of the variability of duration in day math, Moment's API does not officially support adding or subtracting decimal values for days and larger.<br> Moment.js will accept decimal values and do its best to handle them by rounding to the nearest whole number.</p>
<p>As of <strong>2.12.0</strong> decimal day and month values use absolute value/round to convert to integers. This means that 1.5 rounds to 2, and -1.5 rounds to -2.</p>
<pre class="notranslate"><code class="language-js">moment().add(1.5, 'days') == moment().add(2, 'days')
moment().add(-1.5, 'days') == moment().add(-2, 'days') == moment().subtract(1.5, 'days') == moment().subtract(2, 'days')
moment().add(2.3, 'months') == moment().add(2, 'months')
moment().add(-2.3, 'months') == moment().add(-2, 'months') == moment().subtract(2.3, 'months') == moment().subtract(2, 'months')
</code></pre>
<p>Quarters and years are converted to months, and then absolute value/rounded.</p>
<pre class="notranslate"><code class="language-js">moment().add(1.5, 'years') == moment().add(18, 'months')
moment().add(.8, 'years') == moment().add(9.6, 'months') == moment().add(10, 'months')
moment().add(1.5, 'quarters') == moment().add(4.5, 'months') == moment().add(5, 'months')
</code></pre>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/lib-concepts/timezone-offset/" name="/lib-concepts/timezone-offset/"></a>
<h3 class="docs-method-title" id="Time-Zone-vs-Offset"> <a href="#/lib-concepts/timezone-offset/">Time Zone vs Offset</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/00-lib-concepts/03-timezone-offset.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<p>Frequently, people are confused about the difference between time zones and UTC offsets.</p>
<p>A UTC offset is a value that represents how far a particular date and time is from UTC. It is expressed in the format HH:mm most of the time.</p>
<p>A time zone is a geographical region where all people observe a legally mandated standard time.</p>
<p>A time zone usually has more than one offset from UTC due to daylight saving time. Several time zones may have the same offset at some point during the year.<br> For example, the time zones America/Chicago, America/Denver, and America/Belize all have an offset of -06:00 at varying times.<br> For this reason, it is impossible to infer a time zone from just an offset value.</p>
<p>The Moment.js core library provides functionality related to adjusting times based on an offset value.<br> It does not provide support for adjusting dates based on time zone data - this is provided by the Moment TimeZone library.</p>
<p><a href="javascript:window.open('https://stackoverflow.com/tags/timezone/info');" target="_blank" rel="noopener noreferrer">For an in depth description of this issue, see the Stack Overflow tag. <i class="fa fa-external-link"></i></a></p>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/lib-concepts/js-date/" name="/lib-concepts/js-date/"></a>
<h3 class="docs-method-title" id="JavaScript-Date"> <a href="#/lib-concepts/js-date/">JavaScript Date</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/00-lib-concepts/04-js-date.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<p>Moment.js provides a wrapper for the native JavaScript date object.<br> In doing this, Moment.js extends the functionality and also accounts for several deficiencies in the object.</p>
<p>Parsing is notably unpredictable with native date. For instance, suppose I am using a computer in the United States, but I have a date in DD/MM/YYYY format.</p>
<pre class="notranslate"><code class="language-js">var a = new Date('01/12/2016'); //December 1 2016 in DD/MM/YYYY format
//"Tue Jan 12 2016 00:00:00 GMT-0600 (Central Standard Time)"
</code></pre>
<p>There is no good work-around for this behavior with the native Date object.<br> Moment's parser handles it just fine though:</p>
<pre class="notranslate"><code class="language-js">moment('01/12/2016', 'DD/MM/YYYY', true).format()
"2016-12-01T00:00:00-06:00"
</code></pre>
<p>In addition, the ECMA Script 5 Specification makes an unusual assertion about the offset of ISO 8601 dates:</p>
<blockquote>
<p>The value of an absent time zone offset is "Z"</p>
</blockquote>
<p>Effectively what this means is that ISO 8601 dates without an offset are to be treated as UTC values, creating the following oddity:</p>
<pre class="notranslate"><code class="language-js">//US local format
var a = new Date('1/1/2016'); 
//"Fri Jan 01 2016 00:00:00 GMT-0600 (Central Standard Time)"

//ISO 8601
var a = new Date('2016-01-01');
//"Thu Dec 31 2015 18:00:00 GMT-0600 (Central Standard Time)"
</code></pre>
<p>The ES2015 spec fixes this mistake, bringing it in line with the ISO8601 specification, which specifies local time absent of offset.<br> This is in it's own way bad as it has numerous negative back compatibility implications.</p>
<p>With Moment, the date is always interpreted as local time, unless you specify otherwise. This is not something that will change with the adoption of ES2015.</p>
<pre class="notranslate"><code class="language-js">moment('2016-01-01')
//"2016-01-01T00:00:00-06:00"
</code></pre>
<p>Arithmetic is another area where the native Date object is lacking. The Date object actually provides no API for this. Instead, it relies on overflowing date values.<br> Suppose you wanted to add 1 day to April 30, 2016. With the date object you would do the following:</p>
<pre class="notranslate"><code class="language-js">var a = new Date('4/30/2016'); 
a.setDate(a.getDate() + 1);
</code></pre>
<p>This does the trick, but is somewhat unintuitive.<br> Moment provides an API to add/subtract:</p>
<pre class="notranslate"><code class="language-js">moment('4/30/2016', 'MM/DD/YYYY').add(1, 'day')
//"2016-05-01T00:00:00-05:00"
</code></pre>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/lib-concepts/internal-properties/" name="/lib-concepts/internal-properties/"></a>
<h3 class="docs-method-title" id="Internal-Properties"> <a href="#/lib-concepts/internal-properties/">Internal Properties</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/00-lib-concepts/05-internal-properties.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<p>Moment objects have several internal properties that are prefixed with <code class="notranslate">_</code>.</p>
<p>The most commonly viewed internal property is the <code class="notranslate">_d</code> property that holds the JavaScript Date that Moment wrappers.<br> Frequently, developers are confused by console output of the value of <code class="notranslate">_d</code>.<br> Moment uses a technique called epoch shifting that causes this property to sometimes differ from the actual date value that the Moment reflects.<br> In particular if Moment TimeZone is in use, this property will almost never be the same as the actual value that Moment will output from its public <code class="notranslate">.format()</code> function.<br> As such, the values of <code class="notranslate">_d</code> and any other properties prefixed with <code class="notranslate">_</code> should not be used for any purpose.</p>
<p>To print out the value of a Moment, use <code class="notranslate">.format()</code>, <code class="notranslate">.toString()</code> or <code class="notranslate">.toISOString()</code>.</p>
<p>To retrieve a native Date object from Moment, use <code class="notranslate">.toDate()</code>. This function returns a properly shifted date for interaction with third party APIs.</p>
</div>
</article>
<article class="docs-section">
<a class="docs-section-target" id="/parsing/" name="/parsing/"></a>
<h2 class="docs-section-title" id="Parsing-Guide"><a href="#/parsing/">Parsing Guide</a></h2>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/parsing/intro/" name="/parsing/intro/"></a>
<div class="docs-method-prose">
<p>Moment.js has a very flexible and advanced parser that allows a huge range of functionality.<br> The flexibility of the parser also makes it one of the most frequently misused tools of Moment.js.</p>
<p>This section lays out some guidelines about how to correctly use the parser in your situation.</p>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/parsing/local-utc-zone/" name="/parsing/local-utc-zone/"></a>
<h3 class="docs-method-title" id="Local-vs-UTC-vs-Offset"> <a href="#/parsing/local-utc-zone/">Local vs UTC vs Offset</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/01-parsing/01-local-utc-zone.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<p>Moment offers three functions for parsing dates, the basic moment function, moment.utc, and moment.parseZone.</p>
<p>If you wish to interact with a date in the context of the user's local time, use the moment function.</p>
<pre class="notranslate"><code class="language-js">moment('2016-01-01T23:35:01');
</code></pre>
<p>This results in a date with a UTC offset that is the same as the local computer:</p>
<p>"2016-01-01T23:35:01-06:00"</p>
<p>If you wish to interact with the date as a UTC date, use moment.utc:</p>
<pre class="notranslate"><code class="language-js">moment.utc('2016-01-01T23:35:01');
</code></pre>
<p>This results in a date with a utc offset of +0:00:</p>
<p>"2016-01-01T23:35:01+00:00"</p>
<p>If your date format has a fixed timezone offset, use moment.parseZone:</p>
<pre class="notranslate"><code class="language-javascript">moment.parseZone("2013-01-01T00:00:00-13:00");
</code></pre>
<p>This results in a date with a fixed offset:</p>
<p>"2013-01-01T00:00:00-13:00"</p>
<p>Note that if you use moment() or moment.utc() to parse a date with a specified offset, the date will be converted from that offset to either local or UTC:</p>
<p>This date is shifted by 8 hours, moving from +2 to -6 (the offset of the local machine)</p>
<pre class="notranslate"><code class="language-javascript">moment('2016-01-01T00:00:00+02:00').format()
"2015-12-31T16:00:00-06:00"
</code></pre>
<p>This date is shifted by 2 hours, moving from +2 to UTC</p>
<pre class="notranslate"><code class="language-javascript">moment.utc('2016-01-01T00:00:00+02:00').format()
"2015-12-31T22:00:00+00:00"
</code></pre>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/parsing/known-formats/" name="/parsing/known-formats/"></a>
<h3 class="docs-method-title" id="Known-Date-Formats"> <a href="#/parsing/known-formats/">Known Date Formats</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/01-parsing/02-known-formats.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<p>If you know the format of the date string that you will be parsing, it is always the best choice to explicitly specify that format.</p>
<p>Examples:</p>
<pre class="notranslate"><code class="language-js">moment('01/01/2016', 'MM/DD/YYYY')
moment('2016-01-01 11:31:23 PM', 'YYYY-MM-DD hh:mm:ss a')
</code></pre>
<p>If your dates are in an ISO 8601 format, you can use a constant built into moment to indicate that:</p>
<pre class="notranslate"><code class="language-js">moment('2016-01-01 12:25:32', moment.ISO_8601)
</code></pre>
<p>ISO 8601 formats include, but are not limited to:</p>
<pre class="notranslate"><code>2013-02-08               # A calendar date part
2013-W06-5               # A week date part
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09+07:00      # +-HH:mm
</code></pre>
<p><a href="javascript:window.open('https://momentjs.com/docs/#/parsing/string/');" target="_blank" rel="noopener noreferrer"> See the API documentation on parsing strings for a full listing. <i class="fa fa-external-link"></i></a></p>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/parsing/strict-mode/" name="/parsing/strict-mode/"></a>
<h3 class="docs-method-title" id="Strict-Mode"> <a href="#/parsing/strict-mode/">Strict Mode</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/01-parsing/03-strict-mode.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<p>Strict mode is the recommended mode for parsing dates. You should always use strict mode if your code base will allow it.<br> More than half of the parser issues seen on GitHub and Stack Overflow can be fixed by strict mode.</p>
<p>In a later release, the parser will default to using strict mode.</p>
<p>Strict mode requires the input to the moment to exactly match the specified format, including separators. Strict mode is set by passing true as the third parameter to the moment function.</p>
<pre class="notranslate"><code class="language-js">moment('01/01/2016', 'MM/DD/YYYY', true).format()
"2016-01-01T00:00:00-06:00"
moment('01/01/2016 some text', 'MM/DD/YYYY', true).format()
"Invalid date"
</code></pre>
<p>Separator matching:</p>
<pre class="notranslate"><code class="language-js">//forgiving mode
moment('01-01-2016', 'MM/DD/YYYY', false).format()
"2016-01-01T00:00:00-06:00"
//strict mode
moment('01-01-2016', 'MM/DD/YYYY', true).format()
"Invalid date"
</code></pre>
<p>Scenarios fixed by strict mode:</p>
<pre class="notranslate"><code class="language-js">//UUID matches YYYYDDD because it starts with 7 digits
moment('5917238b-33ff-f849-cd63-80f4c9b37d0c', moment.ISO_8601).format()
"5917-08-26T00:00:00-05:00"
//strict mode fails because trailing data exists
moment('5917238b-33ff-f849-cd63-80f4c9b37d0c', moment.ISO_8601, true).format()
"Invalid date"
</code></pre>
<pre class="notranslate"><code class="language-js">//date has out of range value but is parsed anyways
moment('100110/09/2015', 'MM/DD/YYYY').format()
"2015-10-09T00:00:00-05:00"
//strict mode catches out of range issue
moment('100110/09/2015', 'MM/DD/YYYY', true).format()
"Invalid date"
</code></pre>
<pre class="notranslate"><code class="language-js">//wrong date is parsed because non-strict mode ignores data after format
moment('2016-12-31 11:32 PM').format('LT')
"11:32 AM"
//trailing data is noticed
moment('2016-12-31 11:32 PM', moment.ISO_8601, true).format('LT')
"Invalid date"
</code></pre>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/parsing/forgiving-mode/" name="/parsing/forgiving-mode/"></a>
<h3 class="docs-method-title" id="Forgiving-Mode"> <a href="#/parsing/forgiving-mode/">Forgiving Mode</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/01-parsing/04-forgiving-mode.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<p>While strict mode works better in most situations, forgiving mode can be very useful when the format of the string being passed to moment may vary.</p>
<p>A common scenario where forgiving mode is useful is in situations where a third party API is providing the date, and the date format for that API could change.<br> Suppose that an API starts by sending dates in 'YYYY-MM-DD' format, and then later changes to 'MM/DD/YYYY' format.</p>
<p>In strict mode, the following code results in 'Invalid Date' being displayed:</p>
<pre class="notranslate"><code class="language-js">moment('01/12/2016', 'YYYY-MM-DD', true).format()
"Invalid date"
</code></pre>
<p>In forgiving mode using a format string, you get a wrong date:</p>
<pre class="notranslate"><code class="language-js">moment('01/12/2016', 'YYYY-MM-DD').format()
"2001-12-20T00:00:00-06:00"
</code></pre>
<p>The wrong date scenario in forgiving mode is certainly less obvious to the user, but by that token could go unnoticed for a long time.</p>
<p>When choosing between strict and forgiving mode, it is important to consider whether it is more important that dates be accurate, or that dates never display as "Invalid Date".</p>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/parsing/multiple-formats/" name="/parsing/multiple-formats/"></a>
<h3 class="docs-method-title" id="Multiple-Formats"> <a href="#/parsing/multiple-formats/">Multiple Formats</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/01-parsing/05-multiple-formats.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<p>Moment's parser supports specifying multiple possible formats for a date string. This can be extremely useful for situations where a date may be coming from multiple data sources.<br> Just pass the formats as an array:</p>
<pre class="notranslate"><code class="language-js">moment('12 March, 2016', ['DDMMMMY', 'MMMMDDY']).format()
"2016-03-12T00:00:00-06:00"
moment('March 12, 2016', ['DDMMMMY', 'MMMMDDY']).format()
"2016-03-12T00:00:00-06:00"
</code></pre>
<p>In order for this functionality to work properly, moment must parse every format provided. Because of this, the more formats that are used, the longer that parsing takes.<br> Moment's heuristic for determining which format to use is as follows:</p>
<ul>
<li>Prefer formats resulting in <a href="#/parsing/is-valid/">valid</a> dates over invalid ones.</li>
<li>Prefer formats that parse more of the string than less and use more of the format than less, i.e. prefer stricter parsing.</li>
<li>Prefer formats earlier in the array than later.</li>
</ul>
</div>
</article>
<article class="docs-section">
<a class="docs-section-target" id="/warnings/" name="/warnings/"></a>
<h2 class="docs-section-title" id="Warnings-and-Errors"><a href="#/warnings/">Warnings and Errors</a></h2>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/warnings/intro/" name="/warnings/intro/"></a>
<div class="docs-method-prose">
<p>There are several places where Moment.js displays deprecation warnings about functionality that will be removed in the future. Work-arounds are outlined here.</p>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/warnings/js-date/" name="/warnings/js-date/"></a>
<h3 class="docs-method-title" id="JS-Date-Construction"> <a href="#/warnings/js-date/">JS Date Construction</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/02-warnings/01-js-date.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<pre class="notranslate"><code>Moment construction falls back to js Date. 
This is discouraged and will be removed in an upcoming major release.
</code></pre>
<p>This deprecation warning is thrown when no known format is found for a date passed into the string constructor.<br> To work around this issue, specify a format for the string being passed to <code class="notranslate">moment()</code>.</p>
<p><a href="javascript:window.open('https://momentjs.com/docs/#/parsing/');" target="_blank" rel="noopener noreferrer">See the parsing docs for more information. <i class="fa fa-external-link"></i></a></p>
<p><a href="javascript:window.open('https://github.com/moment/moment/issues/1407');" target="_blank" rel="noopener noreferrer">View original GitHub issue <i class="fa fa-external-link"></i></a></p>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/warnings/define-locale/" name="/warnings/define-locale/"></a>
<h3 class="docs-method-title" id="Define-Locale-Override"> <a href="#/warnings/define-locale/">Define Locale Override</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/02-warnings/02-define-locale.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<pre class="notranslate"><code>Use moment.updateLocale(localeName, config) to change an existing locale. 
moment.defineLocale(localeName, config) should only be used for creating a new locale
</code></pre>
<p>This deprecation warning is thrown when you attempt to change an existing locale using the defineLocale function.<br> Doing this will result in unexpected behavior related to property inheritance. moment.updateLocale will properly replace properties on an existing locale.</p>
<p><a href="javascript:window.open('https://github.com/moment/moment/pull/2774');" target="_blank" rel="noopener noreferrer">View original pull request <i class="fa fa-external-link"></i></a></p>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/warnings/parent-locale/" name="/warnings/parent-locale/"></a>
<h3 class="docs-method-title" id="Parent-Locale-Undefined"> <a href="#/warnings/parent-locale/">Parent Locale Undefined</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/02-warnings/03-parent-locale.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<p>Warning removed since <strong>2.16.0</strong>.</p>
<p>A locale can be defined with a parent before the parent itself is defined or loaded. If the parent doesn't exist or can't be lazy loaded when the moment is created, the parent will default to the global locale.</p>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/warnings/locale-not-found/" name="/warnings/locale-not-found/"></a>
<h3 class="docs-method-title" id="Locale-Not-Found"> <a href="#/warnings/locale-not-found/">Locale Not Found</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/02-warnings/04-locale-not-found.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<pre class="notranslate"><code>Locale &lt;key&gt; not found. Did you forget to load it?
</code></pre>
<p>This warning is displayed when a global locale is set but Moment cannot find it. Perhaps this locale is not bundled in your copy.</p>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/warnings/add-inverted-param/" name="/warnings/add-inverted-param/"></a>
<h3 class="docs-method-title" id="Add/Subtract"> <a href="#/warnings/add-inverted-param/">Add/Subtract</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/02-warnings/05-add-inverted-param.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<pre class="notranslate"><code>moment().add(period, number) is deprecated. Please use moment().add(number, period)
moment().subtract(period, number) is deprecated. Please use moment().subtract(number, period)
</code></pre>
<p>Moment deprecated ordering the parameters of add and subtract as (period, number). Invert your parameters.</p>
<p>Bad:</p>
<pre class="notranslate"><code class="language-js">moment().add('hours', 3);
</code></pre>
<p>Good:</p>
<pre class="notranslate"><code class="language-js">moment().add(3, 'hours');
</code></pre>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/warnings/min-max/" name="/warnings/min-max/"></a>
<h3 class="docs-method-title" id="Min/Max"> <a href="#/warnings/min-max/">Min/Max</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/02-warnings/06-min-max.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<pre class="notranslate"><code>moment().min is deprecated, use moment.max
moment().max is deprecated, use moment.min
</code></pre>
<p>This warning is not a typo, but it is confusing.</p>
<p>Previous to version 2.7.0, moment supported moment().min and moment().max functions. These functions were unintuitive.</p>
<p>Min would return the greater of the two moments in question, and max would return the lesser.</p>
<p>Due to this inverted behavior, the suggestion provided in the deprecation warning is correct.</p>
<pre class="notranslate"><code class="language-js">moment('2016-01-01').min('2016-02-01').format()
"2016-02-01T00:00:00-06:00"
//is equivalent to
moment.max(moment('2016-01-01'), moment('2016-02-01')).format()
"2016-02-01T00:00:00-06:00"
</code></pre>
<pre class="notranslate"><code class="language-js">moment('2016-01-01').max('2016-02-01').format()
"2016-01-01T00:00:00-06:00"
//is equivalent to
moment.min(moment('2016-01-01'), moment('2016-02-01')).format()
"2016-01-01T00:00:00-06:00"
</code></pre>
<p><a href="javascript:window.open('https://github.com/moment/moment/issues/1548');" target="_blank" rel="noopener noreferrer">See original GitHub issue. <i class="fa fa-external-link"></i></a></p>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/warnings/zone/" name="/warnings/zone/"></a>
<h3 class="docs-method-title" id="Zone"> <a href="#/warnings/zone/">Zone</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/02-warnings/07-zone.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<pre class="notranslate"><code>moment().zone is deprecated, 
use moment().utcOffset instead.
</code></pre>
<p>This deprecation was made for purposes of clarity.</p>
<p>The result of <code class="notranslate">moment().zone()</code> is an integer that indicates the number of minutes that a given moment is offset from UTC, with the sign inverted (US moments result in a positive value).</p>
<p>Using <code class="notranslate">moment().zone(number)</code> to set the offset will set the offset on the date, also using an inverted sign.</p>
<p>Because a time zone is not the same thing as an offset, the name was changed to utcOffset. At that time the sign was corrected to reflect the actual direction of the UTC offset.</p>
<pre class="notranslate"><code class="language-js">moment().zone()
360
//is replaced by
moment().utcOffset()
-360

moment().zone(420)
//is replaced by 
moment().utcOffset(-420)
</code></pre>
<p><a href="#/lib-concepts/timezone-offset/">For more information on time zone vs offset, see the Time Zone vs Offset guide.</a></p>
<p><a href="javascript:window.open('https://github.com/moment/moment/issues/1779');" target="_blank" rel="noopener noreferrer">View original GitHub issue. <i class="fa fa-external-link"></i></a></p>
</div>
</article>
<article class="docs-section">
<a class="docs-section-target" id="/externalresources/" name="/externalresources/"></a>
<h2 class="docs-section-title" id="External-Resources"><a href="#/externalresources/">External Resources</a></h2>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/externalresources/intro/" name="/externalresources/intro/"></a>
<div class="docs-method-prose">
<p>These resources are made by members of the date/time/time zone community.</p>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/externalresources/videos/" name="/externalresources/videos/"></a>
<h3 class="docs-method-title" id="Videos"> <a href="#/externalresources/videos/">Videos</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/03-externalresources/01-videos.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<p><b class="notranslate"><a href="javascript:window.open('https://www.youtube.com/watch?v=ieIzNP6gKqU');" target="_blank" rel="noopener noreferrer">Date and Time Odds, Ends and Oddities <i class="fa fa-external-link"></i></a></b></p>
<ul>
<li>YouTube, Maggie Pint, Moment.js core contributor</li>
</ul>
<p><b class="notranslate"><a href="javascript:window.open('https://www.pluralsight.com/courses/date-time-fundamentals');" target="_blank" rel="noopener noreferrer">Date and Time Fundamentals <i class="fa fa-external-link"></i></a></b></p>
<ul>
<li>Pluralsight, Matt Johnson, Moment.js core contributor</li>
</ul>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/externalresources/blogs/" name="/externalresources/blogs/"></a>
<h3 class="docs-method-title" id="Blogs"> <a href="#/externalresources/blogs/">Blogs</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/03-externalresources/02-blogs.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<p><b class="notranslate"><a href="javascript:window.open('http://codeofmatt.com/');" target="_blank" rel="noopener noreferrer">Matt Johnson's Blog <i class="fa fa-external-link"></i></a></b></p>
<ul>
<li>Moment.js core contributor. Date and time concepts in programming.</li>
</ul>
<p><b class="notranslate"><a href="javascript:window.open('https://maggiepint.com/');" target="_blank" rel="noopener noreferrer">Maggie Pint's Blog <i class="fa fa-external-link"></i></a></b></p>
<ul>
<li>Moment.js core contributor. Moment.js development progress and roadmaps. Moment.js library help.</li>
</ul>
<p><b class="notranslate"><a href="javascript:window.open('http://www.creativedeletion.com/');" target="_blank" rel="noopener noreferrer">Lau Taarnskov's blog <i class="fa fa-external-link"></i></a></b></p>
<ul>
<li>Author - Elixir Calendar. Date and time programming concepts. Elixir date and time.</li>
</ul>
<p><b class="notranslate"><a href="javascript:window.open('https://codeblog.jonskeet.uk/');" target="_blank" rel="noopener noreferrer">Jon Skeet's blog <i class="fa fa-external-link"></i></a></b></p>
<ul>
<li>Author - NodaTime. Stack Overflow #1 user. Date and time programming concepts.</li>
</ul>
</div>
</article>
<article class="docs-method">
<a class="docs-method-target" id="/externalresources/miscellaneous/" name="/externalresources/miscellaneous/"></a>
<h3 class="docs-method-title" id="Miscellaneous"> <a href="#/externalresources/miscellaneous/">Miscellaneous</a> </h3>
<a class="docs-method-edit" href="javascript:window.open('https://github.com/moment/momentjs.com/blob/master/guides/moment/03-externalresources/03-miscellaneous.md');" target="_blank" rel="noopener noreferrer">edit <i class="fa fa-external-link"></i></a>
<div class="docs-method-prose">
<p><b class="notranslate"><a href="javascript:window.open('https://stackoverflow.com/tags/timezone/info');" target="_blank" rel="noopener noreferrer">Stack Overflow TimeZone Tag Wiki <i class="fa fa-external-link"></i></a></b></p>
<p><b class="notranslate"><a href="javascript:window.open('https://stackoverflow.com/q/4331189');" target="_blank" rel="noopener noreferrer">Stack Overflow DateTime vs DateTimeOffset <i class="fa fa-external-link"></i></a></b></p>
<p><b class="notranslate"><a href="javascript:window.open('https://www.iana.org/time-zones');" target="_blank" rel="noopener noreferrer">IANA Time Zone Database <i class="fa fa-external-link"></i></a></b></p>
<ul>
<li>Main time zone database. This is where Moment TimeZone sources its data from.</li>
</ul>
</div>
</article></div>
</div>
</section>
<div class="right-sidebar">
<div class="affix"><ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6108808167664152" data-ad-slot="3102929424" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
</div>
</div>
<div class="ft">
<a href="#toolbar-title" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a>
<footer class="footer">
<div class="footer__container--normal">
<img alt="扫码关注公众号" title="扫码关注公众号" src="static/picture/qr-code.png" width="170" height="170">
<div class="footer__description--normal">
<p class="paragraph footer__author--normal">Docs4dev<sup class="super">&#xAE;</sup>
</p>
<p class="paragraph footer__quote&#45;&#45;normal">
如果你在使用过程中遇到任何问题，可以在 <a href="javascript:window.open('https://github.com/docs4dev/docs4dev-issues');" target="_blank" rel="noopener noreferrer">这里<i class="fa fa-external-link"></i></a> 提issue。
</p>
<div class="footer__main--normal">
<p class="paragraph footer__main__paragraph--normal copyright" style="color: #666 !important;">
<a href="javascript:window.open('https://beian.miit.gov.cn/');">
蜀ICP备14021783号-6
</a>
</p>
<p class="paragraph footer__main__paragraph--normal copyright" style="color: #666 !important;">
Copyright &#xA9; Docs4dev all
right reserved, powered by <a href="index2.html" target="_blank">Docs4dev</a></p>
</div>
</div>
</div>
<div class="box__issues">
</div>
</footer>
</div>
</div>
</div>
</div>
</div>
<script>
  var hasToc = true;
  /*  var downloadable = /!*false*!/ false;
    var editable = /!*false*!/ false;
    var code = /!*"momentjs"*!/ false;
    var version = /!*"2.24.0"*!/ false;
    var type = /!*"guides"*!/ false;
    var lang = /!*"en"*!/ 'en';
    //edit link
    require(["gitbook", "jQuery"], function (gitbook, $) {
      gitbook.events.bind('start', function (e, config) {
        // Add edit toolbar to left
        var chapterId = /!*24800*!/ 0;
        if (downloadable) {
          gitbook.toolbar.createButton({
            icon: 'fa fa-download',
            text: '下载',
            onClick: function () {
              window.open('/download?code=' + code + '&version=' + version + '&type=' + type + '&lang=' + lang);
            }
          });
        }
        if (editable) {
          gitbook.toolbar.createButton({
            icon: 'fa fa-edit',
            text: '编辑此页',
            onClick: function () {
              window.open('/docs/edit?chapterId=' + chapterId + '&lang=' + lang);
            }
          });
        }
      });
    });*/
</script>
<script>
    var code = "momentjs";
    var lang = "en";
  </script>

<script src="static/js/autocomplete-js.js"></script>
<script src="static/js/app.min.js"></script>
<script src="static/js/search.min.js"></script>
</body>
</html>
